package book.controller;

import book.entity.privileges;
import book.entity.role;
import book.entity.role_privileges;
import book.service.PrivilegesService;
import book.service.RolePrivilegesService;
import book.service.RoleServices;
import netscape.security.Privilege;
import org.apache.xmlbeans.impl.xb.ltgfmt.FileDesc;
import org.omg.Messaging.SYNC_WITH_TRANSPORT;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("privileges")
public class PrivilegesController {

    @Autowired
    RoleServices roleServices;

    @Autowired
    PrivilegesService privilegesService;

    @Autowired
    RolePrivilegesService rolePrivilegesService;

    @RequestMapping(value = "getAll", method = RequestMethod.GET)
    public String getAll(HttpServletRequest request){
//        Map<String,Object> map=new HashMap<String,Object>();
        List<role> list= roleServices.getAll();
        List<privileges> privileges=privilegesService.getAll();
        request.setAttribute("list",list);
        request.setAttribute("privileges",privileges);
        return "role/add";
    }

    @RequestMapping(value = "update", method = RequestMethod.GET)
    public String update(HttpServletRequest request,@RequestParam("id") int id){
        List<privileges> jihe=new ArrayList<privileges>();


        role list= roleServices.getById(id);
        List<privileges> privileges=privilegesService.getAll();
//        for(int i=0;i<privileges.size();i++){
//            if(privileges.get(i).getParent() == null){
//                privileges.remove(i);
//            }
//        }

        for (int k=0;k<list.getRole_privilegesList().size();k++) {
            for(privileges p : privileges){
                if(list.getRole_privilegesList().get(k).getPrivilege().getId() == p.getId()){
                    privileges.remove(p);
                    break;
                }
            }
        }
        request.setAttribute("privileges",privileges);
        request.setAttribute("role",list);
        return "role/add";
    }



    @RequestMapping(value = "getById",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> getById(@RequestParam("id") int id){
        Map<String,Object> map=new HashMap<String,Object>();
        role list= roleServices.getById(id);

        map.put("total",list.getRole_privilegesList().size());
        map.put("rows",list.getRole_privilegesList());
        return map;
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> getAdd(HttpServletRequest request,@RequestParam("shuzu") int[] shuzu,@RequestParam("id") int id){
        Map<String,Object> map=new HashMap<String,Object>();
        role r=new role();
        r.setId(id);
        for (int i=0;i<shuzu.length;i++){
            privileges val=privilegesService.getById(shuzu[i]);
            if(val.getParent() != null){
                role r2=new role();
                r2.setId(id);
                privileges p2=new privileges();
                p2.setId(val.getParent().getId());
                role_privileges rps2=new role_privileges();
                rps2.setRole(r2);
                rps2.setPrivilege(p2);
                role_privileges rps=rolePrivilegesService.getid(rps2);//是否有其父权限  有就不用添加了
                if(rps == null){
                    role_privileges pr=new role_privileges();
                    privileges d=new privileges();
                    d.setId(val.getParent().getId());
                    pr.setPrivilege(d);
                    pr.setRole(r);
                    rolePrivilegesService.insert(pr);
                }

            }
            role_privileges pr=new role_privileges();
            privileges p=new privileges();
            p.setId(shuzu[i]);
            pr.setPrivilege(p);
            pr.setRole(r);
            rolePrivilegesService.insert(pr);
        }
        map.put("result",true);
        return map;
    }

    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> delete(@RequestParam("id") int id){
        Map<String, Object> map = new HashMap<String,Object>();

        rolePrivilegesService.delete(id);
        map.put("result",true);
        return map;
    }
}
